package com.springmvc.core;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
import java.util.HashMap;

import com.springmvc.annotation.Controller;
import com.springmvc.annotation.RequestMapping;

public class reflectionDeal {
	
	Boolean isCon = false;
	
	HashMap<String,Object> hs = new HashMap<>();
	
	HashMap<String,String> requestMap = new HashMap<>();
	
	//反射解析注解
	public void dealByClassName(String name) throws NoSuchFieldException, SecurityException{
		try {
			Class<?> forName = Class.forName(name);
			Controller annotation = forName.getAnnotation(Controller.class);
			String value = annotation.value();
			System.out.println(value);
			//con
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void parseCon(Class<?> forName){
		Controller annotation = forName.getAnnotation(Controller.class);
		if(annotation!=null){
			isCon = true;
		}else{
			isCon = false;
		}
	}
	
	public void insertMap(String name,Class<?> forName){
		if(isCon){
		try {
			Object newInstance = forName.newInstance();
			hs.put(name, newInstance);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		}
	}
	
	public void requestMap(Class<?> forName) throws Exception{
		RequestMapping annotation = forName.getAnnotation(RequestMapping.class);
		String classValue = "";
		if(annotation!=null){
			classValue = classValue + annotation.value();
		}
		String methodValue = "";
		Method[] declaredMethods = forName.getDeclaredMethods();
		if(declaredMethods==null){
			 throw new Exception("declaredMethods==null");
		}
		for (Method method : declaredMethods) {
			RequestMapping annotation2 = method.getAnnotation(RequestMapping.class);
			if(annotation2 != null){
			methodValue = classValue + "/" + annotation2.value();
			String name = method.getName();
			requestMap.put(methodValue, name);
			}
		}
	}
	
	public static void main(String[] args) throws NoSuchFieldException, SecurityException {
		//reflectionDeal reflectionDeal = new reflectionDeal();
		//reflectionDeal.dealByClassName("com.springmvc.controller.mycontroller");
		String name ="/";
		System.out.println(name);
	}
}
